Functions in C

C program is nothing but a combination of one or more functions. Every C program starts with a user-defined function main().

C language supports 2 types of functions. They are

  1. Library Functions
  2. User defined functions

Library functions are pre-defined set of functions. The users can use the functions but cannot modify or change them.
Example printf(), scanf() etc.

User defined functions are defined by the user according to their requirements. The user can modify the functions.

Definition of Function

A function is a self-contained block or a sub program of one or more statements that performs a specific task when called.

Syntax

 function name(argument/parameter list)
{
  variable declaration;
  statement 1;
  statement 2;
  return(value);
} 

There are 3 steps involved defining and using the function.

  • Function Declaration
  • Function Definition
  • Function Call

Syntax

// Function Declaration
int abc(int l, int k); 
void main()
{
-----;
-----;
// Function call x, y are actual arguments
abc(x,y); 
-----
-----
}

// Function Definition
int abc(int l, int k) 
{
-------
----
 return();
}

Function Declaration

Any function should be declared before using it. Function declaration informs the compiler about the function name, parameters is accept, and its return type.

Syntax

returntype functionName(type1 parameter1, type2 parameter2,...);

returntype - Specifies the type of result returned by the function. If nothing will be return by the function we will mention it as void type.

functionName - Its the name of the function. It can be any readable name provided by the user which acts as the identifier.

Argument/Parameter List - The parameter list declares the type and number of arguments that the function expects when it is called.

Example program

#include<stdio.h>
int add(int a, int b);     // function declaration

int main() 
{
    int i, j, result;
    printf("Please enter 2 numbers you want to add...");
    scanf("%d%d", &i, &j);
    
    result = add(i, j);        // function call
    printf("The result is: %d", result);
    return 0;
}

// Function definition.
int add(int a, int b)
{
    return (a*b);       
}

In the above function we have declared a function named add() with 2 parameters. It add the two integer values and returns the result.

Here we get the 2 input from the user and pass it to the add() function and get the result in result variable. Finally we will print the result.

Function Definition

The function definition is the set of statements that are used to perform some task. The function definition should match the function declaration prototype. Function definition may contain three parts.

local variables - Variables declared inside the function.

function body - List of statements that perform some task.

return statement - It returns some result value after performing the task.

Below is the function definition in the above program.

// Function definition.
int add(int a, int b)
{
    return (a*b);       
}

Function Call

Once after declaring and defining the function we can call the function to perform the task with the function name and arguments if any.

Syntax

functionName(argument1, argument2,...);

In the above example add(i, j); is the function call.

Function with and without Arguments

Suppose we need to pass some inputs to the function to perform some tasks. In such cases we will pass those inputs as parameters to the function. It is called as function with argument.

If function does not require inputs to process a task we wont create a function with parameter/argument. It is called as function without argument.

Example program for function with argument

#include<stdio.h>
int add(int a, int b);     // function declaration

int main() 
{
    int i, j, result;
    printf("Please enter 2 numbers you want to add...\n");
    scanf("%d%d", &i, &j);
    
    result = add(i, j);        // function call
    printf("The result is: %d", result);
    return 0;
}

// Function definition.
int add(int a, int b)
{
    return a+b;       
}

Output

Please enter 2 numbers you want to add...
10 40
The result is: 50

In the above function, we have declared a function named add() with 2 parameters. It add the two integer values and returns the result.

Example program for function without argument

#include<stdio.h>
void multiply();    // function declaration

int main() 
{
    // function call
	multiply();        
    return 0;
}

// Function definition.
void multiply()
{
    int i, j, result;
    printf("Please enter 2 numbers you want to multiply...\n");
    scanf("%d%d", &i, &j);
	result = i * j;
	printf("The result is: %d", result);      
}

Output

Please enter 2 numbers you want to multiply...
10 40
The result is: 400

In the above program, we have created a function named multiply to get two input from the user and perform muliplication of the 2 input variables and print the result.

Function with/without return type

A function once after performing the task, can return the result or it may not need to return the result. So we have the provision to return or not return any values after performing the task.

The datatype of the value returned using the return statement should be same as the return type mentioned at function declaration and definition.

Example program for function with return type

#include<stdio.h>
int add(int a, int b);     // function declaration

int main() 
{
    int i, j, result;
    printf("Please enter 2 numbers you want to add...\n");
    scanf("%d%d", &i, &j);
    
    result = add(i, j);        // function call
    printf("The result is: %d", result);
    return 0;
}

// Function definition.
int add(int a, int b)
{
    return a+b;       
}

Example program for function without return type

#include<stdio.h>
void add();     // function declaration

int main() 
{
	// Function call.
	add();
    return 0;
}

// Function definition.
void add()
{
    int i, j, result;
    printf("Please enter 2 numbers you want to add...\n");
    scanf("%d%d", &i, &j);
	result = i+j;
	printf("The result is: %d", result);     
}

Most Read